正则表达式 您所在的位置:网站首页 find 正则表达式 正则表达式

正则表达式

2024-07-11 19:14| 来源: 网络整理| 查看: 265

¥Using regular expressions in JavaScript

正则表达式与 RegExp 方法 test() 和 exec() 以及 String 方法 match()、matchAll()、replace()、replaceAll()、search() 和 split() 一起使用。

¥Regular expressions are used with the RegExp methods test() and exec() and with the String methods match(), matchAll(), replace(), replaceAll(), search(), and split().

方法 描述 exec() 执行字符串中匹配项的搜索。如果不匹配,它会返回一个信息数组或 null。 test() 测试字符串中的匹配。它返回 true 或 false。 match() 返回包含所有匹配项的数组,包括捕获组;如果未找到匹配项,则返回 null。 matchAll() 返回一个包含所有匹配项的迭代器,包括捕获组。 search() 测试字符串中的匹配。它返回匹配的索引,如果搜索失败,则返回 -1。 replace() 在字符串中执行匹配搜索,并用替换子字符串替换匹配的子字符串。 replaceAll() 搜索字符串中的所有匹配项,并用替换子字符串替换匹配的子字符串。 split() 使用正则表达式或固定字符串将字符串分解为子字符串数组。

当你想知道是否在字符串中找到某个模式时,请使用 test() 或 search() 方法;要了解更多信息(但执行速度较慢),请使用 exec() 或 match() 方法。如果你使用 exec() 或 match() 并且匹配成功,这些方法将返回一个数组并更新关联正则表达式对象以及预定义正则表达式对象 RegExp 的属性。如果匹配失败,exec() 方法将返回 null(强制转换为 false)。

¥When you want to know whether a pattern is found in a string, use the test() or search() methods; for more information (but slower execution) use the exec() or match() methods. If you use exec() or match() and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp. If the match fails, the exec() method returns null (which coerces to false).

在以下示例中,脚本使用 exec() 方法在字符串中查找匹配项。

¥In the following example, the script uses the exec() method to find a match in a string.

jsconst myRe = /d(b+)d/g; const myArray = myRe.exec("cdbbdbsbz");

如果你不需要访问正则表达式的属性,则创建 myArray 的另一种方法是使用以下脚本:

¥If you do not need to access the properties of the regular expression, an alternative way of creating myArray is with this script:

jsconst myArray = /d(b+)d/g.exec("cdbbdbsbz"); // similar to 'cdbbdbsbz'.match(/d(b+)d/g); however, // 'cdbbdbsbz'.match(/d(b+)d/g) outputs [ "dbbd" ] // while /d(b+)d/g.exec('cdbbdbsbz') outputs [ 'dbbd', 'bb', index: 1, input: 'cdbbdbsbz' ]

(有关不同行为的更多信息,请参阅 将全局搜索标志与 exec() 一起使用。)

¥(See Using the global search flag with exec() for further info about the different behaviors.)

如果你想从字符串构造正则表达式,还有另一种选择是此脚本:

¥If you want to construct the regular expression from a string, yet another alternative is this script:

jsconst myRe = new RegExp("d(b+)d", "g"); const myArray = myRe.exec("cdbbdbsbz");

使用这些脚本,匹配成功并返回数组并更新下表中显示的属性。

¥With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.

Results of regular expression execution. 对象 属性或索引 描述 在这个例子中 myArray 匹配的字符串和所有记住的子字符串。 ['dbbd', 'bb', index: 1, input: 'cdbbdbsbz'] index 输入字符串中匹配项的从 0 开始的索引。 XSPACE1 input 原来的字符串。 'cdbbdbsbz' [0] 最后匹配的字符。 'dbbd' myRe lastIndex 下一场比赛开始的索引。(仅当正则表达式使用 g 选项时才设置此属性,如 使用标志进行高级搜索 中所述。) XSPACE5 source 图案的文本。在创建正则表达式时更新,而不是执行时更新。 'd(b+)d'

如本示例的第二种形式所示,你可以使用通过对象初始值设定项创建的正则表达式,而不将其分配给变量。但是,如果这样做,则每次出现都是一个新的正则表达式。因此,如果你使用此形式而不将其分配给变量,则随后无法访问该正则表达式的属性。例如,假设你有以下脚本:

¥As shown in the second form of this example, you can use a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:

jsconst myRe = /d(b+)d/g; const myArray = myRe.exec("cdbbdbsbz"); console.log(`The value of lastIndex is ${myRe.lastIndex}`); // "The value of lastIndex is 5"

但是,如果你有这个脚本:

¥However, if you have this script:

jsconst myArray = /d(b+)d/g.exec("cdbbdbsbz"); console.log(`The value of lastIndex is ${/d(b+)d/g.lastIndex}`); // "The value of lastIndex is 0"

两条语句中出现的 /d(b+)d/g 是不同的正则表达式对象,因此其 lastIndex 属性具有不同的值。如果需要访问使用对象初始值设定项创建的正则表达式的属性,则应首先将其分配给变量。

¥The occurrences of /d(b+)d/g in the two statements are different regular expression objects and hence have different values for their lastIndex property. If you need to access the properties of a regular expression created with an object initializer, you should first assign it to a variable.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有